﻿@{
    Layout = null;
}

<!DOCTYPE html>

<html class="x-admin-sm">
<head>
    <meta name="viewport" content="width=device-width" />
    <meta charset="UTF-8">
    <title>基础数据维护</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <link href="~/Content/X-admin/css/font.css" rel="stylesheet" />
    <script src="~/Content/X-admin/lib/layui/layui.js"></script>
    <link href="~/Content/X-admin/css/xadmin.css" rel="stylesheet" />
    <script src="~/Content/X-admin/js/jquery.min.js"></script>
    <script src="~/Content/X-admin/js/xadmin.js"></script>
    <script src="~/Content/Scripts/System/systemFrame.js"></script>
    <script src="~/Content/Scripts/System/Common.js"></script>
    <script type="text/html" id="barDemo">
        <a title="修改" href="javascript:;" lay-event="edit" id="update"><i class="layui-icon">&#xe642;</i></a>
        <a title="删除" href="javascript:;" lay-event="del" id="del"><i class="layui-icon">&#xe640;</i></a>
    </script>
</head>
<body>
    <div class="layui-fluid">
        <div class="layui-row layui-col-space15">
            <div class="layui-col-md12">
                <div class="layui-card">
                    <div class="layui-card-body ">
                        <form class="layui-form layui-col-space5" name="qForm" id="qForm">

                            <div class="layui-inline layui-show-xs-block">
                                <select id="parentCode" name="parentCode" autocomplete="off" lay-filter="gxInfo">
                                    <option value="">数据大类</option>
                                </select>
                            </div>
                            <div class="layui-inline layui-show-xs-block">
                                <select id="ctype" class="tClass" name="ctype" autocomplete="off" lay-filter="parInfo">
                                    <option value="">数据类型</option>
                                </select>
                            </div>

                            <div class="layui-inline layui-show-xs-block">
                                <button class="layui-btn" lay-filter="sreach" id="btn-view" type="button" onclick="loadData()"><i class="layui-icon">&#xe615;</i></button>
                            </div>
                        </form>
                    </div>


                    <div class="layui-card-header">
                        <button class="layui-btn" id="btn-update" type="button" onclick="openWin()"><i class="layui-icon"></i>添加</button>
                    </div>


                    <div class="layui-card-body layui-table-body layui-table-main">
                        <table class="layui-hide" id="list" lay-filter="list"></table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>

<script type="text/javascript">
    $(function () {
        WebPowersShow();
        CommonTop();

        layui.use('form', function () {
            var form = layui.form;
            form.on('select(gxInfo)', function (data) {
                CommonType();
            });
            form.on('select(parInfo)', function (data) {
                //loadData("3");
                loadData();
            });
            form.render();
        });
    })
    /**
    加载数据
    **/
    function loadData(par) {
        var par = "3";
        layui.use('table', function () {
            var param = new Object();
            param.cType = $("#ctype").val();
            if (par == null || par == "") {
                param.parentCode = $("#parentCode").val();
            } else {
                param.parentCode = par;
            }
            var condition = JSON.stringify(param);
            var table = layui.table;
            //给table赋值
            table.render({
                elem: '#list'
                , url: '../Basis/GetDataList?condition=' + condition + "&dataType=cmd" //数据接口
                , title: '数据列表'
                , page: true //开启分页
                , even: true
                , limits: [20, 50, 100]
                , limit: 20 //每页默认显示的数量
                , cols: [[ //表头
                    { field: 'cCode', align: 'center', title: '编码', width: '14%', fixed: 'left' }
                    , { field: 'cName', align: 'center', title: '名称', width: '20%', edit: 'text' }
                    , { field: 'cType', align: 'center', title: '所属类型', width: '20%' }
                    , { field: 'cMemo', align: 'center', title: '属性值', width: '20%' }
                    , { field: 'createtime', align: 'center', title: '创建时间', width: '20%' }
                    , { field: 'right', align: 'center', title: '操作', width: '6%', align: 'center', toolbar: '#barDemo' }
                ]], done: function (res, curr, count) {
                    $("[data-field='id']").css('display', 'none');//隐藏列
                    TablePowerHide();
                }
            });

            //监听行工具事件
            table.on('tool(list)', function (obj) { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
                var data = obj.data //获得当前行数据
                    , layEvent = obj.event; //获得 lay-event 对应的值
                if (layEvent == 'del') {
                    layer.confirm('确认要删除该数据？', function (index) {
                        $.ajax({
                            url: "../Basis/DelCommonData",
                            type: "post",
                            data: { dataId: data.id },
                            success: function (data) {
                                if (data.Result) {
                                    layer.msg("删除成功");
                                } else {
                                    layer.msg("删除失败");
                                }
                                loadData();
                            }
                        });
                        layer.close(index);
                    });

                }
                else if (layEvent == 'edit') {
                    var gvalue = $('#parentCode').val();
                    var fvalue = $("#ctype").val();
                    InfoOpenPage("../Basis/CommonDataUpdate?dataId=" + data.id + "&gvalue=" + gvalue + "&fvalue=" + fvalue, "基础数据更新", "75%", "90%");
                }
            });

            //监听单元格编辑
            table.on('edit(list)', function (obj) {
                var value = obj.value //得到修改后的值
                    , data = obj.data //得到所在行所有键值
                    , field = obj.field; //得到字段
                $.ajax({
                    url: "../Basis/EditCommonNames",
                    data: { dataId: data.id, names: value },
                    type: "post",
                    success: function (data) {
                        if (!data.Result) {
                            layer.msg(data.Message);
                        }

                    }
                })

            });
        });
    }

    function openWin() {
        var gvalue = $('#parentCode').val();
        var fvalue = $("#ctype").val();
        if (gvalue == "") {
            layer.msg("请先选择类型"); return;
        }
        InfoOpenPage("../Basis/CommonDataUpdate?dataId=0&gvalue=" + gvalue + "&fvalue=" + fvalue, "基础数据更新", "75%", "90%");
    }
    /**
    数据类型
    **/
    function CommonType() {
        var tCode = $("#parentCode").val();
        var cStr = "";
        $.ajax({
            url: "../Basis/getCommonDataType",
            dataType: "json",
            data: { type: tCode },
            type: "POST",
            async: false,
            success: function (data) {
                if (data.Result) {
                    var datas = data.Data;
                    cStr += "<option value=''>选择数据</option>";
                    for (var i = 0; i < datas.length; i++) {
                        cStr += "<option value=\"" + datas[i]["ctype"] + "\" >" + datas[i]["cmemo"] + "(" + datas[i]["ctype"] + ")" + "</option>";
                    }
                    $("#ctype").empty();
                    $("#ctype").append(cStr);
                    layui.use('form', function () {
                        var form = layui.form;
                        form.render();
                    });
                }
            }
        });
    }

    function CommonTop() {
        $.ajax({
            url: "../Basis/getCommonDataTop",
            dataType: "json",
            type: "POST",
            async: false,
            success: function (data) {
                if (data.Result) {
                    var datas = data.Data;
                    for (var i = 0; i < datas.length; i++) {
                        $("#parentCode").append("<option value=\"" + datas[i].cCode + "\" >" + datas[i].cName + "</option>");
                    }
                    layui.use('form', function () {
                        var form = layui.form;
                        form.render();
                    });
                }
            }
        });
    }
</script>
